package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.scity.beens.LoadResponse;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    TextView textView;
    EditText edtUser;
    EditText edtPsw;
    Button bntLoad;
    LoadResponse loadResponse;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Toast.makeText(LoginActivity.this,loadResponse.msg,Toast.LENGTH_LONG).show();
                    if(loadResponse.code==200){
                        Configs.Token = loadResponse.token;
                        Log.d(Configs.TAG,Configs.Token);
                        finish();
                    }

                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        InitReg();

        InitViews();
    }

    private void InitViews() {
        edtPsw = findViewById(R.id.login_psw);
        edtUser = findViewById(R.id.login_user);
        bntLoad = findViewById(R.id.login_load);

        bntLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkHttpClient okHttpClient = new OkHttpClient();
                HashMap<String,String> map = new HashMap<>();
                map.put("username",edtUser.getText().toString());
                map.put("password",edtPsw.getText().toString());
                MediaType mediaType = MediaType.parse("application/json");//请求数据类型
                RequestBody requestBody = RequestBody.create(mediaType,new Gson().toJson(map));//请求体
                //请求对象
                Request request = new Request.Builder().url(Configs.Host+APIInterface.LoadInterface)
                        .post(requestBody).build();//
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        loadResponse = new Gson().fromJson(response.body().string(),LoadResponse.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        });
    }

    private void InitReg() {
        textView = findViewById(R.id.login_newuser);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegActivity.class));
            }
        });
    }
}